import { createStore } from 'vuex'

const store = createStore({
    state: {
        imgUrl:'https://wjapi.surveydiscovery.com/',
        isMobile: false,
        active: 0,
        userInfo: null,
        isLoggedIn: false,
        token: null,
        language: 'en', // 添加语言状态，默认英文
        shopName: null, // 添加店铺名称
        shopData: null,  // 添加店铺数据
        userInfoK:{
            avatar: '',
            is_wj: 1,
            nick_name: '',
            real_name: '',
            token: '',
            uid: '',
            username: ''
        }
    },
    mutations: {
        setUserInfo(state, userInfo) {
            console.log('Setting user info:', userInfo)
            state.userInfo = userInfo
            state.isLoggedIn = true
            // 保存用户信息到 localStorage
            if (userInfo) {
                localStorage.setItem('userInfo', JSON.stringify(userInfo))
                console.log('User info saved to localStorage')
            } else {
                localStorage.removeItem('userInfo')
                console.log('User info removed from localStorage')
            }
        },
        setToken(state, token) {
            console.log('Setting token:', token)
            state.token = token
            // 保存到 localStorage
            if (token) {
                localStorage.setItem('token', token)
                console.log('Token saved to localStorage')
            } else {
                localStorage.removeItem('token')
                console.log('Token removed from localStorage')
            }
        },
        logout(state) {
            state.userInfo = null
            state.isLoggedIn = false
            state.token = null
            localStorage.removeItem('token')
            localStorage.removeItem('userInfo')
        },
        initializeAuth(state) {
            console.log('Initializing auth from localStorage')
            // 从 localStorage 恢复登录状态
            const token = localStorage.getItem('token')
            const userInfo = localStorage.getItem('userInfo')
            
            console.log('Found token in localStorage:', !!token)
            console.log('Found userInfo in localStorage:', !!userInfo)
            
            if (token && userInfo) {
                try {
                    state.token = token
                    state.userInfo = JSON.parse(userInfo)
                    state.isLoggedIn = true
                    console.log('Auth restored successfully:', state.userInfo)
                } catch (e) {
                    console.error('Failed to parse user info:', e)
                    // 如果解析失败，清除无效数据
                    localStorage.removeItem('token')
                    localStorage.removeItem('userInfo')
                }
            } else {
                console.log('No auth data found in localStorage')
            }
        },
        // 添加设置语言的mutation
        setLanguage(state, language) {
            state.language = language
            localStorage.setItem('language', language)
        },
        // 初始化语言设置
        initializeLanguage(state) {
            // 从 localStorage 恢复语言设置
            const savedLanguage = localStorage.getItem('language');
            if (savedLanguage) {
                state.language = savedLanguage;
            } else {
                // 如果没有保存的语言，默认设置为英文
                state.language = 'en';
                localStorage.setItem('language', 'en');
            }
        },
        // 添加设置店铺信息的mutation
        setShopInfo(state, { shopName, shopData }) {
            state.shopName = shopName
            state.shopData = shopData
            // 保存到 localStorage
            if (shopName) {
                localStorage.setItem('shopName', shopName)
            }
            if (shopData) {
                localStorage.setItem('shopData', JSON.stringify(shopData))
            }
        },
        // 初始化店铺信息
        initializeShopInfo(state) {
            const savedShopName = localStorage.getItem('shopName')
            const savedShopData = localStorage.getItem('shopData')
            if (savedShopName) {
                state.shopName = savedShopName
            }
            if (savedShopData) {
                try {
                    state.shopData = JSON.parse(savedShopData)
                } catch (e) {
                    console.error('Failed to parse shop data:', e)
                }
            }
        }
    },
    actions: {
        async login({ commit }, { userInfo, token }) {
            commit('setUserInfo', userInfo)
            commit('setToken', token)
        },
        async logout({ commit }) {
            commit('logout')
        },
        initializeAuth({ commit }) {
            commit('initializeAuth')
        },
        // 添加设置语言的action
        setLanguage({ commit }, language) {
            commit('setLanguage', language)
        },
        // 添加切换语言的action
        toggleLanguage({ commit, state }) {
            const newLanguage = state.language === 'zh' ? 'en' : 'zh'
            commit('setLanguage', newLanguage)
        },
        // 初始化语言
        initializeLanguage({ commit }) {
            commit('initializeLanguage')
        },
        // 添加设置店铺信息的action
        setShopInfo({ commit }, shopInfo) {
            commit('setShopInfo', shopInfo)
        },
        // 初始化店铺信息
        initializeShopInfo({ commit }) {
            commit('initializeShopInfo')
        }
    },
    getters: {
        isLoggedIn: state => state.isLoggedIn,
        userInfo: state => state.userInfo,
        token: state => state.token,
        // 添加语言getter
        currentLanguage: state => state.language,
        isChinese: state => state.language === 'zh',
        isEnglish: state => state.language === 'en',
        // 添加店铺信息getter
        shopName: state => state.shopName,
        shopData: state => state.shopData
    }
})

export default store
